package org.video.mine.mycode;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.video.mine.R;
import org.video.mine.adapter.MovieInfoAdapter;
import org.video.mine.adapter.RecyDownAdapter;
import org.video.mine.entity.MovieDetailEntity;
import org.video.mine.entity.MoviePlayerEntity;

import java.util.ArrayList;
import java.util.List;

public class ShowMessageDialog extends Dialog {

    LinearLayout llPositive;
    TextView tvCancel;
    TextView tvTitle;
    RecyclerView rcyDown;
    ClickListener clickListener;
    Context context;
    private RecyDownAdapter adapter;
    private List<MoviePlayerEntity> loadPlayLists=new ArrayList<>();
    private List<MoviePlayerEntity> AddloadPlayLists=new ArrayList<>();

    public ShowMessageDialog(@NonNull Context context, boolean showPositive) {
        this(context,R.style.dialog_message,showPositive);
    }

    public ShowMessageDialog(@NonNull Context context, int themeResId, boolean showPositive) {
        super(context, themeResId);
        init(context, showPositive);
    }

    protected ShowMessageDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    private void init(Context context, boolean showPositive) {
        this.context = context;
        setCancelable(true);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        View view = LayoutInflater.from(context).inflate(R.layout.message_dialog,null);
        llPositive = view.findViewById(R.id.ll_positive);
        tvTitle = view.findViewById(R.id.tv_title);
        rcyDown = view.findViewById(R.id.rcy_down);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),3);
        rcyDown.setLayoutManager(gridLayoutManager);
        adapter = new RecyDownAdapter(loadPlayLists,context);
        rcyDown.setAdapter(adapter);
        adapter.setOnItemClick(new RecyDownAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                MoviePlayerEntity moviePlayerEntity = loadPlayLists.get(position);
                if (moviePlayerEntity.isCheck()){
                    loadPlayLists.get(position).setCheck(false);
                    AddloadPlayLists.remove(moviePlayerEntity);
                }else {
                    loadPlayLists.get(position).setCheck(true);
                    AddloadPlayLists.add(moviePlayerEntity);
                }
                adapter.notifyItemChanged(position,"check");
            }
        });

        tvCancel = view.findViewById(R.id.tv_cancel);
        if (showPositive){
            llPositive.setVisibility(View.VISIBLE);
        }else {
            llPositive.setVisibility(View.GONE);
        }

        llPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener!=null){
                    clickListener.clickPositive(AddloadPlayLists);
                }
                dismiss();
            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener!=null){
                    clickListener.clickCancel();
                }
                dismiss();
            }
        });
        setContentView(view);
    }

    public void setTvTitle(String title){
        if (tvTitle!=null){
            tvTitle.setText(title);
        }
    }

    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setRecyView(List<MoviePlayerEntity> playLists) {
        if (rcyDown!=null){
            for (MoviePlayerEntity detailEntity:playLists){
                detailEntity.setCheck(false);
            }
            loadPlayLists.clear();
            AddloadPlayLists.clear();
            loadPlayLists.addAll(playLists);
            adapter.notifyDataSetChanged();
        }
    }

    public interface ClickListener{
        void clickPositive(List<MoviePlayerEntity> addloadPlayLists);
        void clickCancel();
    }

}
